package com.lanou.dllo.baisibudejie.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.dllo.baisibudejie.R;
import com.lanou.dllo.baisibudejie.volley.DiskCache;

import java.io.File;

/**
 * Created by dllo on 15/10/30.
 */
public class MyCardActivity extends Activity implements View.OnClickListener {

    private TextView myCard_backTv, report_statusTv, repost_ContentTv, report_voiceTv;
    private ImageView report_imageIv;
    private Bitmap bitmap;
    private File myRecAudioFile;
//    private boolean sdCardExit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_mycard);

        initView();//初始化界面
        initData();//初始化数据
    }

    //初始化数据
    private void initData() {
//        /* 判断SD Card是否插入 */
//        sdCardExit = Environment.getExternalStorageState().equals(
//                android.os.Environment.MEDIA_MOUNTED);
//
//        /* 取得SD Card路径作为录音的文件位置 */
//        if (sdCardExit) {
//            myRecAudioDir = Environment.getExternalStorageDirectory();
//        }
//        /* 取得SD Card目录里的所有.amr文件 */
//        getRecordFiles();
        myRecAudioFile = new File("/storage/emulated/0", "new.amr");
        Intent intent = getIntent();

        if (intent != null) {
            //判断是否发送成功
            if (intent.getStringExtra("text") != null && intent.getStringExtra("text").length() > 0
                    && intent.getStringExtra("bitmap").length() > 0) {
                String content = intent.getStringExtra("text");
                report_statusTv.setText("发表成功");
                repost_ContentTv.setText(content);
                String uri = intent.getStringExtra("bitmap");
                DiskCache cache = new DiskCache();
                bitmap = cache.getBitmap(uri);//取出本地文件
                report_imageIv.setImageBitmap(bitmap);

            } else if (intent.getStringExtra("content") != null && intent.getStringExtra("content").length() > 0) {
                String content = intent.getStringExtra("content");
                report_statusTv.setText("发表成功");
                repost_ContentTv.setText(content);
            } else {
                report_statusTv.setText("发表失败");
            }
            if (intent.getBooleanExtra("isVoice", false)) {

                report_voiceTv.setText(myRecAudioFile.getName());
            }
        }


    }

    //初始化页面
    private void initView() {

        myCard_backTv = (TextView) findViewById(R.id.myCard_backTv);
        myCard_backTv.setOnClickListener(this);
        report_statusTv = (TextView) findViewById(R.id.report_statusTv);
        repost_ContentTv = (TextView) findViewById(R.id.repost_ContentTv);
        report_imageIv = (ImageView) findViewById(R.id.report_imageIv);
        report_voiceTv = (TextView) findViewById(R.id.report_voiceTv);
        report_voiceTv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.myCard_backTv://返回
                finish();
                break;
            case R.id.report_voiceTv://发表声音
                openFile(myRecAudioFile);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    /* 打开播放录音文件的程序 */
    private void openFile(File f) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(android.content.Intent.ACTION_VIEW);
        String type = getMIMEType(f);
        intent.setDataAndType(Uri.fromFile(f), type);
        startActivity(intent);
    }

    private String getMIMEType(File f) {
        String end = f.getName().substring(f.getName().lastIndexOf(".") + 1,
                f.getName().length()).toLowerCase();
        String type = "";
        if (end.equals("mp3") || end.equals("aac") || end.equals("aac")
                || end.equals("amr") || end.equals("mpeg")
                || end.equals("mp4")) {
            type = "audio";
        } else if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg")) {
            type = "image";
        } else {
            type = "*";
        }
        type += "/*";
        return type;
    }

}
